﻿using ChurchDiary.BLL;
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChurchDiary.TableEntity;
using ChurchDiary.Common;
using System.Web.Security;
using System.Xml;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace ChurchWebsite.Controllers
{
    public class HomeController : Controller
    {
        BLL_User blluser = new BLL_User();
        public ActionResult Index()
        {
            Session.Abandon();
            Response.Cookies.Clear();
            return View();
        }
        public ActionResult Login()
        {
            if (this.Session["username"] == null)
            {
                string username = Request.Form["txtusername"].ToString();
                string password = Request.Form["txtpassword"].ToString();
                EntityUser User = blluser.GetUserInfo(username, password);
                if (User.Userid != null)
                {
                    if (User.Status == 2)
                    {
                        return RedirectToAction("EmailVerificationPending", "Home", new { UserId = User.Userid });
                    }
                    Current.Image = User.Image;
                    Current.Name = User.Name;
                    Current.ChurchId = User.ChurchId;
                    Current.IsSuperAdmin = User.IsSuperAdmin;
                    Current.Userid = User.Userid;
                    Current.LanaguageId = User.UserSettings.LanguageId;
                    Current.UserImage = User.UserSettings.UserPicture;
                    Current.CoverImage = User.UserSettings.CoverPicture;
                    if (User.UserLoginTypes.Count == 1)
                    {
                        EntityUserType userType = blluser.GetUserType(User.UserLoginTypes[0].UserTypeId);
                        return RedirectToAction(userType.ActionName, userType.ControllerName, new { area = userType.AreaName });
                    }
                    else if(User.UserLoginTypes.Count >= 2)
                    {
                        return RedirectToAction("MultipleLogin", "Home", new { UserId = User.Userid });
                    }
                    else
                    {
                        return RedirectToAction("LoginFailed", "Home");
                    }
                }
                else
                {
                    return RedirectToAction("LoginFailed", "Home");
                }
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        public ActionResult EmailVerificationPending(Guid userId)
        {
            ViewBag.UserInfo = blluser.GetUserInfo(userId);
            ViewBag.SoftWareInfo = new BLL_Organization().GetSoftwareInfo();
            ViewBag.OrganizationInfo = new BLL_Organization().GetOrganizationInfo();
            return View();
        }
        public ActionResult ResendActivationMail(Guid userId)
        {
            ViewBag.UserInfo = blluser.GetUserInfo(userId);
            ViewBag.SoftWareInfo = new BLL_Organization().GetSoftwareInfo();
            ViewBag.OrganizationInfo = new BLL_Organization().GetOrganizationInfo();
            EntityEmail email = new BLL_Common().GetEmailsByUserId(userId, "LOGIN");
            ViewBag.Result = new BLL_Common().SendEmail(email);
            return View();
        }
        public ActionResult ActivateAccount(Guid userId)
        {
            EntityUser user = blluser.GetUserInfo(userId);
            ViewBag.UserInfo = blluser.GetUserInfo(userId);
            ViewBag.OrganizationInfo = new BLL_Organization().GetOrganizationInfo();
            ViewBag.Result = new BLL_Common().ActivateUserAccount(userId);
            return View();
        }
        public ActionResult MultipleLogin(Guid userId)
        {
            ViewBag.UserInfo = blluser.GetUserInfo(userId);
            ViewBag.OrganizationInfo = new BLL_Organization().GetOrganizationInfo();
            return View();
        }
        public ActionResult LoginFailed()
        {
            ViewBag.OrganizationInfo = new BLL_Organization().GetOrganizationInfo();
            return View();
        }
        public ActionResult MultipleLoginRedirect(Guid UserTypeId)
        {
            EntityUserType userType = blluser.GetUserType(UserTypeId);
            return RedirectToAction(userType.ActionName, userType.ControllerName, new { area = userType.AreaName });
        }
    }
}
